package com.yanzhu.service.impl;

import com.yanzhu.service.VideoService;
import com.yanzhu.service.mapper.GrabRedPacketsMapper;
import com.yanzhu.service.mapper.LikeMapper;
import com.yanzhu.service.mapper.RedPacketsMapper;
import com.yanzhu.service.mapper.VideoMapper;
import com.yanzhu.service.model.RedPackets;
import com.yanzhu.service.model.Video;
import com.yanzhu.service.model.mix.VideoHot;
import com.yanzhu.service.model.mix.VideoInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 10:10 2018/1/19
 * @Modified:
 */
@AllArgsConstructor
@Service
public class VideoServiceImpl implements VideoService {

    private final VideoMapper videoMapper;

    private final LikeMapper likeMapper;

    private final RedPacketsMapper redPacketsMapper;

    private final GrabRedPacketsMapper grabRedPacketsMapper;

    @Override
    public int insert(Video video) {
        return videoMapper.insert(video);
    }

    @Override
    public List<VideoHot> selectByUid(long uid, int page, int size) {
        int begin = (page - 1) * size;
        return videoMapper.selectByUid(uid, begin, size);
    }

    @Override
    public String selectVideoPathById(long vid) {
        return videoMapper.selectVideoPathById(vid);
    }

    @Override
    public List<VideoHot> queryHotVideo(int view, int page, int size) {
        int begin = (page - 1) * size;
        return videoMapper.queryHotVideo(view, begin, size);
    }

    @Override
    public void updateVideoView(long vid) {
        videoMapper.updateVideoView(vid);
    }

    @Override
    public void updateVideo(Video video) {
        videoMapper.updateVideo(video);
    }

    @Override
    public Video selectById(long id) {
        return videoMapper.selectById(id);
    }

    @Override
    public VideoInfo queryVideoInfo(long vid, long uid) {
        Video video = videoMapper.selectById(vid);
        List<String> avatars = likeMapper.queryVideoLikeUserAvatar(vid, 5);//点赞前五名头像
        int count = likeMapper.queryVideoLikeUserCount(vid);//点赞人数
        RedPackets redPackets = redPacketsMapper.queryVideoRedPackets(vid);//视频红包情况
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setVideo(video);
        videoInfo.setAvatars(avatars);
        videoInfo.setCount(count);
        videoInfo.setRedPackets(redPackets);
        if(grabRedPacketsMapper.queryRedPacketsByUid(uid) != null)
            videoInfo.setIs_grab(-1);
        return videoInfo;
    }

    @Override
    public int deleteVideoById(long id) {
        return videoMapper.deleteVideoById(id);
    }

    @Override
    public int deleteVideoAll(long uid) {
        return videoMapper.deleteVideoAll(uid);
    }
}
